<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<%
	/* 获取页面跳转的数据 */
	String username=request.getParameter("username"); //用户
	String password=request.getParameter("password"); //密码
	String confimpassword=request.getParameter("confirmPassword"); //重复密码
	String gender=request.getParameter("sex"); //性别
	String telNum=request.getParameter("iphone"); //联系电话
	String email=request.getParameter("email"); //邮箱
	String realName=request.getParameter("realName"); //真实姓名
%>
<body>
<%
try{
	String driverName="com.mysql.jdbc.Driver";
	String url="jdbc:mysql://localhost:3306/hotel";
	String userName="root";
	String passWord="root";
	
	Class.forName(driverName);
	Connection con2=DriverManager.getConnection(url,userName,passWord);
	String sql2="select * from adinfo where adname=?";
	PreparedStatement st2=con2.prepareStatement(sql2);
	st2.setString(1,username);
	//执行sql语句
	ResultSet rs2=st2.executeQuery();
	if(rs2.next()){ //先判断数据是否存在数据库
		out.print("该账号已经注册过......");
	}else{
		Connection con3=DriverManager.getConnection(url,userName,passWord);
		//定义发射器
		String sql3="insert into user(uname,pwd,telNum,realName,gender,email) VALUES ( ?,?,?,?,?,?)";
		PreparedStatement st3=con3.prepareStatement(sql3);
		st3.setString(1, username);
		st3.setString(2, password);
		st3.setString(3, telNum);
		st3.setString(4, realName);
		st3.setString(5, gender);
		st3.setString(6, email);
	
		Connection con4=DriverManager.getConnection(url,userName,passWord);
		//定义发射器
		String sql4="insert into adinfo(adname,adpwd,adlevel) VALUES ( ?,?,?)";
		PreparedStatement st4=con4.prepareStatement(sql4);
		st4.setString(1, username);
		st4.setString(2, password);
		st4.setInt(3, 0);

		if(password.equals(confimpassword)&&!username.equals(null)){
			int num= st3.executeUpdate();
			
			if(num>0){
				st4.executeUpdate();
			//	out.print("注册成功了哦!");
				String urls="Login.jsp";
				response.sendRedirect(urls);
			}else{
				out.print("注册失败!");
			}
		}else{
			out.print("两次密码输入不一致!");
		}
		if (con3 != null && !con3.isClosed()) {
	        con3.close();
	      }
	}
	
	if (con2 != null && !con2.isClosed()) {
        con2.close();
      }
}catch(ClassNotFoundException e) {
    e.printStackTrace();
  } catch (SQLException e) {
    e.printStackTrace();
  
}


%>

</body>
</html>